package com.csdj.t309.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.params.SetParams;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class JedisLianJieChi {
    static class Inner{
        static JedisPool pool;
        static {
            try {
                pool = initpool();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   public JedisLianJieChi(String host,String port,String password){

   }
    private JedisLianJieChi(){}
   private static JedisPool initpool() throws IOException {
        InputStream is=JedisLianJieChi.class.getClassLoader().getResourceAsStream("Redis.properties");
       Properties properties=new Properties();
       properties.load(is);
       String host=properties.getProperty("host","localhost");
       String port=properties.getProperty("port","6379");
       String password=properties.getProperty("password","123456");
       JedisPoolConfig config=new JedisPoolConfig();
       config.setMaxTotal(20);
       config.setMinIdle(2);
       System.out.println(host);
       return new JedisPool(config,host,Integer.parseInt(port),3000,password);
   }
    public static Jedis lianjie(){
       return Inner.pool.getResource();
    }
    private static String getstring(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().get(key);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static long delstring(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().del(key);
            }
            return 0;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static boolean existsstring(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().exists(key);
            }
            return false;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static long expirestring(String key,long time) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().expire(key,time);
            }
            return 1;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }private static long ttlstring(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().ttl(key);
            }
            return 0;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }private static long decrstring(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().decr(key);
            }
            return 0;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }private static String setstring(String key,String value) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            SetParams setParams=new SetParams();
            setParams.ex(1);//过期时间
            if (resoure != null) {
                return lianjie().set(key,value);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }private static String hgetHash(String name,String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().hget(name,key);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static Map hgetAllHash(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().hgetAll(key);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static long hSetHash(String name,String key,String value) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().hset(name,key,value);
            }
            return 1;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static List<String> bLpopList(int time, String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().blpop(time,key);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static List<String> bRpopList(int time, String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().brpop(time,key);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static String bRpopLpushList(String key,String value,int time) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().brpoplpush(key,value,time);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
    private static String lindexList(String key,long index) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().lindex(key,index);
            }
            return null;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }
//    private static String linsertList(String name,List list,String key,String value) {
//        Jedis resoure = null;
//        try {
//            resoure = lianjie();
//            if (resoure != null) {
//                return lianjie().linsert(name,list,key,value);
//            }
//            return null;
//        } finally {
//            if (resoure != null) {
//                resoure.close();
//            }
//        }
//    }
    private static long lenList(String key) {
        Jedis resoure = null;
        try {
            resoure = lianjie();
            if (resoure != null) {
                return lianjie().llen(key);
            }
            return 0;
        } finally {
            if (resoure != null) {
                resoure.close();
            }
        }
    }



}
